package com.bw.xiaosy.shiro;

import com.bw.xiaosy.redis.RedisSessionDao;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * shiro配置
 *
 * @author Xiaosy
 * @date 2017-04-06 14:49
 */
@Component
public class ShiroConfiguration {

    @Autowired
    private MyShiroRealm myShiroRealm;
    @Autowired
    private RedisSessionDao redisSessionDao;

    /**
     * 注册securityManager
     * @return
     */
    @Bean
    public SecurityManager securityManager()
    {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myShiroRealm);
        securityManager.setSessionManager(sessionManager());
        return securityManager;
    }

    @Bean
    public SessionManager sessionManager()
    {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.getSessionIdCookie().setName("JSID");
        sessionManager.setSessionDAO(redisSessionDao);
        sessionManager.setGlobalSessionTimeout(7200000);
        return sessionManager;
    }

    /**
     * 注册ShiroFilter
     * @param securityManager
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager)
    {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        Map<String,Filter> filterMap = new LinkedHashMap<>();
        //自己的过滤器替代authc过滤器
        filterMap.put("authc",loginFilter());
        //添加自己的过滤器
        filterMap.put("perms",myAuthorizationFilter());
        shiroFilterFactoryBean.setFilters(filterMap);

        Map<String,String> filterChainDefinitionMap = new HashMap<>();
        filterChainDefinitionMap.put("/**","authc");
        filterChainDefinitionMap.put("/userInfo","perms[admin:listUser]");
        filterChainDefinitionMap.put("/addUser","perms[test1:addUser]");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);//设置过滤器链


        return  shiroFilterFactoryBean;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager)
    {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
    /**
     * 配置自己的登陆过滤器
     * @return
     */
    @Bean
    public LoginFilter loginFilter()
    {
        return new LoginFilter();
    }

    /**
     * 配置自己的验证过滤器
     * @return
     */
    @Bean
    public MyAuthorizationFilter myAuthorizationFilter()
    {
        return new MyAuthorizationFilter();
    }
}
